*********************************************************************************
			*****  Appendix E - Analysis *****
*********************************************************************************

*********************************************************************************
***** Set-up
*********************************************************************************

cap log close
clear all
timer clear
version 15.1
set more off
set matsize 10000

global path "C:\Users\Prof. Wiederhold\Desktop\Projects\RYL - Dokumente\Replication" //enter path where combined PIAAC/PIAAC-L data are stored

// Folder and paths
foreach folder in orig data prog log table figure results {
  global `folder' ${path}\\`folder'
}

// Load data
use "$data\AppendixE_PIAAC.dta", clear 

** generate low-SES variables based on books, parental employment, and first-gen migrant status

gen nonmigrant_firstGen = 1 - migrant_first

gen books_gr_coarse=1
replace books_gr_coarse=0 if books_12==1|books_12==2 //0-25 books
replace books_gr_coarse=. if books_12==.
label var books_gr_coarse "High-SES (>25 books at home at age 15)"
tab books_gr_coarse

gen uni_parents = (uni_father == 1 | uni_mother == 1)
//note that in PIAAC/PIAAC-L, we only know whether parents actually have a university degree, not whether they have just attended university

#delimit ;
gen HigherSES =  nonmigrant_firstGen ==1 & 
				(books_gr_coarse == 1 | uni_parents == 1) &
				(books_gr_coarse == 1 | nonsingleparent_inclstep_14 == 1) //assumption: you had both parents at the age of 15 if you lived the full 15 years with them
;
#delimit cr

** recode grades

foreach var of varlist grade_math_14 grade_german_14 grade_foreign_14 {
cap drop `var'_recode
recode `var'  (1 = 6 "very good") (2 = 5 "good") (3 = 4 "Satisfactory") (4 = 3 "Sufficient") (5 = 2 "Insufficient") (6 = 1 "Fail") (0 = 99 "Missing or don't know"), gen(`var'_recode)
label var `var'_recode "`var' (recoded from worst to best)"
tab `var'_recode, m
}

** standardization of 2012 skills (largest sample) on estimation sample

foreach var of varlist pvnum1_12 pvlit1_12 pvpsl1_12 {
sum `var' if grade_math_14_recode!=., d 
gen ZZ`var'=(`var'-r(mean))/r(sd) if grade_math_14_recode!=.
sum ZZ`var' 
label var ZZ`var' "`var' (standardized)"
}

** standardization of grades on the estimation sample

foreach var  of varlist grade_math_14_recode grade_german_14_recode grade_foreign_14_recode {
		qui sum `var' if `var'!=99 & ZZpvnum1_12!=.
		gen ZZ`var' = (`var' - `r(mean)')/`r(sd)'  if `var'!=99 & ZZpvnum1_12!=.
		replace ZZ`var' = 99 if `var'==99 & ZZpvnum1_12!=. 
		label var ZZ`var' "`var' (standardized)"
}

** standardization of non-cognitive skills on the numeracy (2012) sample --> i.e., only anchor persons included

foreach var  of varlist bfi_extra_14 bfi_neur_14 bfi_open_14 bfi_con_14 bfi_agree_14 grit_14 inloc_14 exloc_14 risk_attitude_14 trust_14 {
		qui sum `var' if ZZpvnum1_12!=.&`var'_miss!=1
		gen ZZ`var' = (`var' - `r(mean)')/`r(sd)'  if ZZpvnum1_12!=.&`var'_miss!=1
		replace ZZ`var'=99 if ZZpvnum1_12!=.&`var'_miss==1  
		label var ZZ`var' "`var' (standardized)"
		gen ZZ`var'_miss=0 if ZZpvnum1_12!=.&`var'_miss!=1
		replace ZZ`var'_miss=1 if ZZpvnum1_12!=.&`var'_miss==1
		label var ZZ`var'_miss "`var' (standardized) is missing in numeracy (2012) sample"
		replace ZZ`var'=. if missing(level_secondary)  
}

** label variables

la var unemployed_12 "Unemployed (2012)"
la var nonemployed_12 "Unemployed/out-of-labor-force (2012)"
la var log_trim_earnings_12 "Monthly wage (2012)"
la var log_trim_earn_hour_12 "Hourly wage (2012)"

** generate qualification outcomes (highest qualification obtained)

* PIAAC 2012

gen noqualification_12=0
replace noqualification_12=1 if professional_qualification_12==1
replace noqualification_12=. if professional_qualification_12==.|professional_qualification_12==.a
tab noqualification_12

gen noqualification_12_ageabove25=0 if age_12>25
replace noqualification_12_ageabove25=1 if professional_qualification_12==1 & age_12>25
replace noqualification_12_ageabove25=. if professional_qualification_12==.|professional_qualification_12==.a
tab noqualification_12_ageabove25

gen noqualification_12_ageabove30=0 if age_12>30
replace noqualification_12_ageabove30=1 if professional_qualification_12==1 & age_12>30
replace noqualification_12_ageabove30=. if professional_qualification_12==.|professional_qualification_12==.a
tab noqualification_12_ageabove30

gen noqualification_12_ageabove35=0 if age_12>35
replace noqualification_12_ageabove35=1 if professional_qualification_12==1 & age_12>35
replace noqualification_12_ageabove35=. if professional_qualification_12==.|professional_qualification_12==.a
tab noqualification_12_ageabove35

gen noqualification_12_ageabove40=0 if age_12>40
replace noqualification_12_ageabove40=1 if professional_qualification_12==1 & age_12>40
replace noqualification_12_ageabove40=. if professional_qualification_12==.|professional_qualification_12==.a
tab noqualification_12_ageabove40

gen apprenticeship_12_broad=0
replace apprenticeship_12_broad=1 if professional_qualification_12==2|professional_qualification_12==3|professional_qualification_12==4|professional_qualification_12==5
replace apprenticeship_12_broad=. if professional_qualification_12==.|professional_qualification_12==.a
tab apprenticeship_12_broad

gen university_12_broad=0
replace university_12_broad=1 if professional_qualification_12==6|professional_qualification_12==7|professional_qualification_12==8|professional_qualification_12==9|professional_qualification_12==10
replace university_12_broad=. if professional_qualification_12==.|professional_qualification_12==.a
tab university_12_broad

gen dropout_apprenticeship_12=0
replace dropout_apprenticeship_12=1 if uncompleted_qualification_12==1|uncompleted_qualification_12==2|uncompleted_qualification_12==3|uncompleted_qualification_12==4
replace dropout_apprenticeship_12=. if currently_in_education_12==1 //question on uncompleted qualification was not asked to people who are currently in education
replace dropout_apprenticeship_12=. if uncompleted_qualification_12==.|professional_qualification_12==.a //missing for partners first surveyed in PIAAC-L
tab dropout_apprenticeship_12

gen dropout_university_12=0
replace dropout_university_12=1 if uncompleted_qualification_12==5|uncompleted_qualification_12==6|uncompleted_qualification_12==7|uncompleted_qualification_12==8|uncompleted_qualification_12==9
replace dropout_university_12=. if currently_in_education_12==1 //question on uncompleted qualification was not asked to people who are currently in education
replace dropout_university_12=. if uncompleted_qualification_12==.|professional_qualification_12==.a //missing for partners first surveyed in PIAAC-L
tab dropout_university_12
//note that we could alternatively use "dropout_college_12", which was generated by Fanni (this variable only measures dropout if the respective qualification was not obtained later on)

* PIAAC-L 2014 (numbers are larger than those for 2014 because they also include the partner)

gen apprenticeship_14_broad=0
replace apprenticeship_14_broad=1 if apprenticeship_14==1|vocationalschool_14==1|technicalschool_14==1|civilservanttraining_14==1
replace apprenticeship_14_broad=. if apprenticeship_14==.&vocationalschool_14==.&technicalschool_14==.&civilservanttraining_14==.
tab apprenticeship_14_broad

gen university_14_broad=0
replace university_14_broad=1 if technicalcollege_14==1|university_14==1
replace university_14_broad=. if technicalcollege_14==.&university_14==.
tab university_14_broad

gen age_12_sq=age_12*age_12

*********************************************************************************
***** Table E1: Math Grades at School and Later-Life Outcomes
*********************************************************************************

// Analysis set-up
global SE "r"
global basic "c.age_12 age_12_sq female" 
global noncog bfi_extra_14 bfi_neur_14 bfi_open_14 bfi_con_14 bfi_agree_14 grit_14 inloc_14 exloc_14 risk_attitude_14 trust_14

global numskills_12 "ZZpvnum1_12" 
label var ZZpvnum1_12 "Numeracy skills"

global litskills_12 "ZZpvlit1_12" 
label var ZZpvlit1_12 "Literacy skills"

global ictskills_12 "ZZpvpsl1_12" 
label var ZZpvpsl1_12 "ICT skills"

* numeracy skills
reg $numskills_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg $numskills_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss i.level_secondary   $basic, ${SE} 
		
* literacy skills
reg $litskills_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg $litskills_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss  i.level_secondary   $basic, ${SE} 

* ICT skills
reg $ictskills_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg $ictskills_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss  i.level_secondary   $basic, ${SE} 

* unemployment
reg unemployed_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg unemployed_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss i.level_secondary   $basic, ${SE} 	
		
* monthly wages
reg log_trim_earnings_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg log_trim_earnings_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss i.level_secondary   $basic, ${SE} 			
		
* hourly wages
reg log_trim_earn_hour_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg log_trim_earn_hour_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss i.level_secondary   $basic, ${SE} 

*********************************************************************************
***** Table E2: Grit and Labor-Market Outcomes
*********************************************************************************

// Analysis set-up
global SE "r"
global basic "c.age_12 age_12_sq female" 
global noncog_nogrit ZZbfi_open_14 ZZbfi_con_14 ZZbfi_extra_14 ZZbfi_agree_14 ZZbfi_neur_14 ZZinloc_14 ZZexloc_14 ZZrisk_attitude_14 ZZtrust_14 ///
					 ZZbfi_open_14_miss ZZbfi_con_14_miss ZZbfi_extra_14_miss ZZbfi_agree_14_miss ZZbfi_neur_14_miss ZZinloc_14_miss ZZexloc_14_miss ZZrisk_attitude_14_miss ZZtrust_14_miss 

* unemployment
reg unemployed_12 c.ZZgrit_14##i.b0.HigherSES ZZgrit_14_miss $basic, ${SE} 
reg unemployed_12 c.ZZgrit_14##i.b0.HigherSES ZZgrit_14_miss $basic $noncog_nogrit, ${SE} 


* monthly wages
reg log_trim_earnings_12 c.ZZgrit_14##i.b0.HigherSES ZZgrit_14_miss $basic, ${SE} 
reg log_trim_earnings_12 c.ZZgrit_14##i.b0.HigherSES ZZgrit_14_miss $basic $noncog_nogrit, ${SE} 

* hourly wages		
reg log_trim_earn_hour_12 c.ZZgrit_14##i.b0.HigherSES ZZgrit_14_miss $basic, ${SE} 
reg log_trim_earn_hour_12 c.ZZgrit_14##i.b0.HigherSES ZZgrit_14_miss $basic $noncog_nogrit, ${SE} 

*********************************************************************************
***** Table E3: Trust and Labor-Market Outcomes
*********************************************************************************

// Analysis set-up
global SE "r"
global basic "c.age_12 age_12_sq female"  
global noncog_notrust ZZbfi_open_14 ZZbfi_con_14 ZZbfi_extra_14 ZZbfi_agree_14 ZZbfi_neur_14 ZZgrit_14 ZZinloc_14 ZZexloc_14 ZZrisk_attitude_14 ///
				      ZZbfi_open_14_miss ZZbfi_con_14_miss ZZbfi_extra_14_miss ZZbfi_agree_14_miss ZZbfi_neur_14_miss ZZgrit_14_miss ZZinloc_14_miss ZZexloc_14_miss ZZrisk_attitude_14_miss

* unemployment
reg unemployed_12 c.ZZtrust_14##i.b0.HigherSES ZZtrust_14_miss $basic, ${SE} 
reg unemployed_12 c.ZZtrust_14##i.b0.HigherSES ZZtrust_14_miss $basic $noncog_notrust, ${SE} 

* monthly wages
reg log_trim_earnings_12 c.ZZtrust_14##i.b0.HigherSES ZZtrust_14_miss $basic, ${SE} 
reg log_trim_earnings_12 c.ZZtrust_14##i.b0.HigherSES ZZtrust_14_miss $basic $noncog_notrust, ${SE} 

* hourly wages		
reg log_trim_earn_hour_12 c.ZZtrust_14##i.b0.HigherSES ZZtrust_14_miss $basic, ${SE} 
reg log_trim_earn_hour_12 c.ZZtrust_14##i.b0.HigherSES ZZtrust_14_miss $basic $noncog_notrust, ${SE} 

*********************************************************************************
***** Table E4: Professional Qualifications by SES Background
*********************************************************************************

preserve

keep if anchor==1

bys HigherSES: sum  noqualification_12_ageabove25 noqualification_12_ageabove30 noqualification_12_ageabove35 noqualification_12_ageabove40 [w=weight_12]
bys HigherSES: sum  dropout_apprenticeship_12 apprenticeship_12_broad  if age_12>30 [w=weight_12]
bys HigherSES: sum  dropout_university_12 university_12_broad  if age_12>30 [w=weight_12]

* calculate p-value of difference 

svyset [pw=weight_12]

svy: proportion noqualification_12_ageabove25 , over(HigherSES) coefl
test _b[1.noqualification_12_ageabove25@0bn.HigherSES] = _b[1.noqualification_12_ageabove25@1.HigherSES]

svy: proportion noqualification_12_ageabove30 , over(HigherSES) coefl
test _b[1.noqualification_12_ageabove30@0bn.HigherSES] = _b[1.noqualification_12_ageabove30@1.HigherSES]

svy: proportion noqualification_12_ageabove35 , over(HigherSES) coefl
test _b[1.noqualification_12_ageabove35@0bn.HigherSES] = _b[1.noqualification_12_ageabove35@1.HigherSES]

svy: proportion noqualification_12_ageabove40 , over(HigherSES) coefl
test _b[1.noqualification_12_ageabove40@0bn.HigherSES] = _b[1.noqualification_12_ageabove40@1.HigherSES]

svy: proportion dropout_apprenticeship_12 if age_12>30  , over(HigherSES) coefl
test _b[1.dropout_apprenticeship_12@0bn.HigherSES] = _b[1.dropout_apprenticeship_12@1.HigherSES]

svy: proportion apprenticeship_12_broad if age_12>30  , over(HigherSES) coefl
test _b[1.apprenticeship_12_broad@0bn.HigherSES] = _b[1.apprenticeship_12_broad@1.HigherSES]

svy: proportion dropout_university_12 if age_12>30  , over(HigherSES) coefl
test _b[1.dropout_university_12@0bn.HigherSES] = _b[1.dropout_university_12@1.HigherSES]

svy: proportion university_12_broad if age_12>30  , over(HigherSES) coefl
test _b[1.university_12_broad@0bn.HigherSES] = _b[1.university_12_broad@1.HigherSES]

restore

*********************************************************************************
***** Table E5: Professional Qualifications by SES Background: Individuals with Lower-Track Secondary Education
*********************************************************************************

preserve

keep if anchor==1

keep if level_secondary==2|level_secondary==3

bys HigherSES: sum  noqualification_12_ageabove25 noqualification_12_ageabove30 noqualification_12_ageabove35 noqualification_12_ageabove40 [w=weight_12]
bys HigherSES: sum  dropout_apprenticeship_12 apprenticeship_12_broad   if age_12>30 [w=weight_12]
bys HigherSES: sum  dropout_university_12 university_12_broad  if age_12>30 [w=weight_12]

* calculate p-value of difference 

svyset [pw=weight_12]

svy: proportion noqualification_12_ageabove25 , over(HigherSES) coefl
test _b[1.noqualification_12_ageabove25@0bn.HigherSES] = _b[1.noqualification_12_ageabove25@1.HigherSES]

svy: proportion noqualification_12_ageabove30 , over(HigherSES) coefl
test _b[1.noqualification_12_ageabove30@0bn.HigherSES] = _b[1.noqualification_12_ageabove30@1.HigherSES]

svy: proportion noqualification_12_ageabove35 , over(HigherSES) coefl
test _b[1.noqualification_12_ageabove35@0bn.HigherSES] = _b[1.noqualification_12_ageabove35@1.HigherSES]

svy: proportion noqualification_12_ageabove40 , over(HigherSES) coefl
test _b[1.noqualification_12_ageabove40@0bn.HigherSES] = _b[1.noqualification_12_ageabove40@1.HigherSES]

svy: proportion dropout_apprenticeship_12 if age_12>30  , over(HigherSES) coefl
test _b[1.dropout_apprenticeship_12@0bn.HigherSES] = _b[1.dropout_apprenticeship_12@1.HigherSES]

svy: proportion apprenticeship_12_broad if age_12>30  , over(HigherSES) coefl
test _b[1.apprenticeship_12_broad@0bn.HigherSES] = _b[1.apprenticeship_12_broad@1.HigherSES]

svy: proportion dropout_university_12 if age_12>30  , over(HigherSES) coefl
test _b[1.dropout_university_12@0bn.HigherSES] = _b[1.dropout_university_12@1.HigherSES]

svy: proportion university_12_broad if age_12>30  , over(HigherSES) coefl
test _b[1.university_12_broad@0bn.HigherSES] = _b[1.university_12_broad@1.HigherSES]

restore

*********************************************************************************
***** Table E6: Math Grades at School and Professional Qualifications
*********************************************************************************

preserve

//restrict analysis to persons above age 30, who can reasonably have already finished their qualification
keep if age_12>30

// Analysis set-up
global SE "r"
global basic "c.age_12 age_12_sq female"
global noncog bfi_extra_14 bfi_neur_14 bfi_open_14 bfi_con_14 bfi_agree_14 grit_14 inloc_14 exloc_14 risk_attitude_14 trust_14

* no qualification
reg noqualification_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg noqualification_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss i.level_secondary   $basic, ${SE} 
		
* apprenticeship completion
reg apprenticeship_12_broad c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg apprenticeship_12_broad c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss  i.level_secondary   $basic, ${SE} 

* apprenticeship dropout
reg dropout_apprenticeship_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg dropout_apprenticeship_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss  i.level_secondary   $basic, ${SE} 

restore

*********************************************************************************
***** Table E7: Math Grades at School and Professional Qualifications: Individuals with Lower-Track Secondary Education
*********************************************************************************

preserve

keep if age_12>30
keep if level_secondary==2|level_secondary==3

// Analysis set-up
global SE "r"
global basic "c.age_12 age_12_sq female"
global noncog bfi_extra_14 bfi_neur_14 bfi_open_14 bfi_con_14 bfi_agree_14 grit_14 inloc_14 exloc_14 risk_attitude_14 trust_14

* no qualification
reg noqualification_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg noqualification_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss i.level_secondary   $basic, ${SE} 
		
* apprenticeship completion
reg apprenticeship_12_broad c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg apprenticeship_12_broad c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss  i.level_secondary   $basic, ${SE} 

* apprenticeship dropout
reg dropout_apprenticeship_12 c.ZZgrade_math_14_recode##i.b0.HigherSES grade_math_14_miss i.level_secondary   $basic, ${SE} 
reg dropout_apprenticeship_12 c.ZZgrade_math_14_recode##i.b0.HigherSES c.ZZgrade_german_14_recode##i.b0.HigherSES c.ZZgrade_foreign_14_recode##i.b0.HigherSES grade_math_14_miss grade_german_14_miss grade_foreign_14_miss  i.level_secondary   $basic, ${SE} 

restore